package com.molyfun.parents.fragment.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.molyfun.parents.R;


/**
 * Created by ligang on 2016/7/14 0014.
 */
public class MUpDateDialogFragment extends DialogFragment implements View.OnClickListener{

    private Dialog dialog;
    private Button mSureBtn;

    public static MUpDateDialogFragment newInstance(String title,String message){
        MUpDateDialogFragment mUpDateDialogFragment=new MUpDateDialogFragment();
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        bundle.putString("message",message);
        mUpDateDialogFragment.setArguments(bundle);
        return mUpDateDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_update_menu, null);
        builder.setView(view);
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        String title=getArguments().getString("title");
        String message=getArguments().getString("message");
        String temp = message.replace("\\n", "\n");
        ((TextView)view.findViewById(R.id.tv_update_dialog_title)).setText(title);
        ((TextView)view.findViewById(R.id.tv_update_dialog_message)).setText(temp);
        mSureBtn = (Button)view.findViewById(R.id.bt_update_dialog_sure);
        mSureBtn.setOnClickListener(this);
        view.findViewById(R.id.bt_update_dialog_cancel).setOnClickListener(this);
        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_update_dialog_sure:
                if (upDateDialogListener!=null){
                    upDateDialogListener.onSure();
                    mSureBtn.setEnabled(false);
                }
                break;
            case R.id.bt_update_dialog_cancel:
                if (upDateDialogListener!=null){
                    upDateDialogListener.onCancel();
                }
                break;
        }
    }

    public void setDialogListener(UpDateDialogListener listener){
        this.upDateDialogListener=listener;
    }

    private UpDateDialogListener upDateDialogListener;
    public interface UpDateDialogListener {
        void onSure();
        void onCancel();
    }

    
    
    public static class UpDateDialogBuilder{
        MUpDateDialogFragment mUpDateDialogFragment;
        String mTag;

        public UpDateDialogBuilder(String title,String message) {
            mUpDateDialogFragment=MUpDateDialogFragment.newInstance(title,message);
            mTag="update_dialog";
        }
        public UpDateDialogBuilder setUpDateCallBack(UpDateDialogListener upDateDialogListener){
            mUpDateDialogFragment.setDialogListener(upDateDialogListener);
            return this;
        }
        public void build(FragmentTransaction fragmentTransaction){
            fragmentTransaction.add(mUpDateDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }
        public void dismiss(){
            mUpDateDialogFragment.dismiss();
        }
    }
}
